package com.fzsf.drawingboard.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.fzsf.drawingboard.app.MyApplication
import com.fzsf.drawingboard.bluetooth.SimpleFZWritingPadCallBackListener
import com.fzsf.drawingboard.utils.ToastUtil
import com.fzsf.drawingboard.R
import kotlinx.android.synthetic.main.activity_multi_writing.*

class MultiWritingActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_multi_writing)

        setWriteCallBack()
        moveCenter.setOnClickListener {
            multiWritingBoard.move2Center()
//            multiWritingBoard.crop2Center()
        }

        saveBitmap.setOnClickListener {
            saveBitmap()
        }

        startMultiWriting.setOnClickListener {
            startActivity(Intent(MainActivityByKt@ this, MultiRecyclerActivity::class.java))
        }
    }

    private fun setWriteCallBack() {
        var writingPad = MyApplication.getInstance().writingPad
        writingPad?.let {
            if (it.connectState) {
                it.setFZWritingPadCallBackListener(object : SimpleFZWritingPadCallBackListener() {
                    override fun onWriting(x: Int, y: Int, z: Int, time: Long) {
                        runOnUiThread {
                            multiWritingBoard.writeByWritingPad(x, y, z)
                        }
                    }

                    override fun onButtonClick(buttonType: String?) {
                        super.onButtonClick(buttonType)
                        runOnUiThread {
                            multiWritingBoard.reSetBoard()
                        }
                    }
                })
            }
        }

    }

    private fun saveBitmap() {
        ToastUtil.showMessage("保存结果")
        val imagePath = multiWritingBoard.crop2Save()
        Glide.with(this).load(imagePath).asBitmap().into(ivResult)
    }
}
